﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThouShaltNotBrick.Models;
using System.Xml.Linq;
using System.IO;
using GMLogging;

namespace ThouShaltNotBrick.DataAccess
{
    public static class BrickTypesXmlReader
    {
        #region Methods
        public static BrickTypesData Read(string pBrickTypesFilePath)
        {
            BrickTypesData brickTypesData = null;
            FileStream stream = null;
            try
            {
                stream = new FileStream(pBrickTypesFilePath, FileMode.Open, FileAccess.Read);
                XDocument brickTypesXDocument = XDocument.Load(stream);
                brickTypesData = Parse(brickTypesXDocument);
            }
            catch (Exception e)
            {
                Logger.Error(e.Message);
                throw;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }

            return brickTypesData;
        }

        public static BrickTypesData Parse(string pBrickTypesInputString)
        {
            try
            {
                XDocument brickTypesXDocument = XDocument.Parse(pBrickTypesInputString);
                return Parse(brickTypesXDocument);
            }
            catch (Exception e)
            {
                Logger.Error(e.Message);
                throw;
            }
        }

        private static BrickTypesData Parse(XDocument pBrickTypesXDocument)
        {
            XElement brickTypesXElement = pBrickTypesXDocument.Element(BrickTypesXmlConstants.TAG_BRICK_TYPES);

            List<XElement> brickTypeXElements = brickTypesXElement.Elements(BrickTypesXmlConstants.TAG_BRICK_TYPE).ToList();
            List<BrickTypeData> brickTypeList = new List<BrickTypeData>();
            foreach (XElement brickTypeXElement in brickTypeXElements)
            {
                XAttribute brickTypeNameXAttribute = brickTypeXElement.Attribute(BrickTypesXmlConstants.TAG_BRICK_TYPE_ATTRIBUTE_NAME);
                string brickTypeName = brickTypeNameXAttribute.Value;

                XElement brickStatesXElement = brickTypeXElement.Element(BrickTypesXmlConstants.TAG_BRICK_STATES);

                List<XElement> brickStateXElements = brickStatesXElement.Elements(BrickTypesXmlConstants.TAG_BRICK_STATE).ToList();
                List<string> brickStateList = new List<string>();
                foreach (XElement brickStateXElement in brickStateXElements)
                {
                    XAttribute brickStateNameXAttribute = brickStateXElement.Attribute(BrickTypesXmlConstants.TAG_BRICK_STATE_ATTRIBUTE_NAME);
                    string brickStateName = brickStateNameXAttribute.Value;
                    brickStateList.Add(brickStateName);
                }

                BrickTypeData brickType = new BrickTypeData(brickTypeName, brickStateList);
                brickTypeList.Add(brickType);
            }

            BrickTypesData brickTypes = new BrickTypesData(brickTypeList);
            return brickTypes;
        }
        #endregion
    }
}